// 制冷站房用冷趋势图&饼图 分两种情况，根据参数 param.zone (1为左区域，2为右区域)，跟前端沟通好请求
var timeDataMap = {
  'year': [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12],
  'month': [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
  'day': [00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
  'hour': [00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10,
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
    31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
    51, 52, 53, 54, 55, 56, 57, 58, 59]
};
var timeFormatMap = {
  'year': "%Y-%m",
  'month': "%Y-%m-%d",
  'day': "%Y-%m-%d %H",
  'hour': "%Y-%m-%d %H:%m"
};
var timeSingleFormatMap = {
  'year': "%m",
  'month': "%d",
  'day': "%H",
  'hour': "%m"
};

dev.action = function (resp, param) {
  var timeType = iot.getTimeByFormat(param.time);
  var timeDataList = timeDataMap[timeType];
  var format = timeFormatMap[timeType];
  var singleFormate = timeSingleFormatMap[timeType];

  var db = iot.db();
  var itemSql = "select name from iot_virtual_item where dev_id = " + param.id;
  var itemResult = db.qry(itemSql);
  if (param.zone == 1) {
    var jsData = {
      legend: iot.list(),
      xaxis: iot.list(),
      yaxis: iot.list()
    };
    var result;
    // 添加属性数据
    itemResult.forAll(function (item) {
      var itemData = {};
      itemData.type = item.name
      itemData.data = iot.list()
      jsData.yaxis.add(itemData)
      jsData.legend.add(item.name)
    })
    if (itemResult) {
      result = db.qry("select data_type item, DATE_FORMAT(the_time,'" + format + "') time," +
        " DATE_FORMAT(the_time,'" + singleFormate + "') timeIx, sum(dvalue) data" +
        " from iot_dev_data_win where dev_id = " + param.id +
        " and data_type in (" + itemSql + ") and the_time like '" + param.time + "%' group by data_type, time");
    }
    // 添加时间
    for (var ix in timeDataList) {
      jsData.xaxis.add(timeDataList[ix]);
    }
    // 添加数据
    for (var i = 0; i < jsData.legend.length; i++) {
      for (var ix in timeDataList) {
        var data = 0;
        result.forAll(function (item) {
          if (item && jsData.legend[i].equals(item.item) && item.timeIx == timeDataList[ix]) {
            data = item.data;
          }
        })
        jsData.yaxis[i].data.add(data);
      }
    }
    resp.setJsData(jsData);

  } else {
    var jsData = {
      legend: iot.list(),
      datas: iot.list()
    };
    var result;
    if (itemResult) {
      result = db.qry("select data_type item, sum(dvalue) data from iot_dev_data_win where dev_id = " + param.id +
        " and data_type in (" + itemSql + ") and the_time like '" + param.time + "%' group by data_type");
    }
    if (itemResult) {
      itemResult.forAll(function (item1) {
        jsData.legend.add(item1.name);
        var data = 0;
        result.forAll(function (item2) {
          if (item2 && item1.name.equals(item2.item)) {
            data = item2.data;
          }
        })
        jsData.datas.add({name: item1.name, value: data})
      })
    }
    resp.setJsData(jsData);
  }
}